package com.qf.thirdapp.controller;

import com.qf.thirdapp.pojo.AccessTokenInfo;
import com.qf.thirdapp.pojo.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Resource
    private RestTemplate restTemplate;

    private String resoure_url = "http://127.0.0.1:8089/userinfo";

    @RequestMapping
    public String login(String code, Map<String, Object> map) {
        System.out.println("code: " + code);

        String accessTokenUrl = "http://127.0.0.1:8080/oauth/token?client_id=a&client_secret=1" +
                "&grant_type=authorization_code&code="
                + code + "&redirect_uri=http://localhost:7070/login";

        System.out.println(accessTokenUrl);

        AccessTokenInfo accessTokenInfo = restTemplate.getForObject(accessTokenUrl, AccessTokenInfo.class);

        String accessToken = accessTokenInfo.getAccess_token(); //获取access_token

        UserInfo userInfo = restTemplate.getForObject(resoure_url + "?access_token=" + accessToken, UserInfo.class);

        map.put("userinfo", userInfo);
        System.out.println(userInfo.getProfile());

        return "index";
    }

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
    }
}
